package com.ofek.eventos;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.eventos.error.ErrorReport;
import com.google.gson.Gson;
import com.ofek.eventos.db.EventosDatabase;
import com.ofek.eventos.db.datatypes.UserCredential;

public class EventosApplication extends Application {
	public static UserCredential EVENTOS_USER = null;
	public static Context context;
	public static EventosDatabase eventosDatabase;
	public static final String KEY_USER = "user_key";
	public static final boolean DEBUG = false;
	private final boolean delete = true;

	@Override
	public void onCreate() {
		context = getApplicationContext();
		ErrorReport.init(this);
		super.onCreate();
		init();
		final String userJson = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString(KEY_USER, null);
		if ( userJson!=null ) {
			EventosApplication.EVENTOS_USER = new Gson().fromJson(userJson, UserCredential.class);
			eventosDatabase = new EventosDatabase(EventosApplication.EVENTOS_USER.id, "5.29.150.186", 7879);
		}
	}

	private void init() {
		if (!delete) return;
		Constants.EVENTOS_DB_FILE.delete();
		Constants.EVENTOS_REMOTE_DB_FILE.delete();
		Constants.EVENTOS_REQUEST_DB_FILE.delete();
		Constants.EVENTOS_REQUEST_DB_FILE.delete();
		Constants.EVENTOS_CONTEXT_DB_FILE.delete();
		final SharedPreferences shPrefs = PreferenceManager.getDefaultSharedPreferences(EventosApplication.context);
		shPrefs.edit().clear().commit();
	}






}
